package com.loe.ui.view

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ScrollView
import androidx.annotation.Keep
import androidx.core.content.ContextCompat
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.loe.ui.*
import com.loe.ui.api.IUiViewGroup
import com.loe.ui.api.ScrollViewApi
import com.loe.ui.page.UiPage
import com.quickjs.JSArray
import com.quickjs.JSObject

@Keep
open class UiScrollView : ScrollView, IUiViewGroup
{
    constructor(context: Context) : super(context)

    override var uiPage: UiPage? = null

    override val jsObject by lazy { ScrollViewApi(this).jsObject }

    override fun setUI(ui: JSObject, uiPage: UiPage?): View
    {
        this.uiPage = uiPage
        // click
        ui.gotHasBoolean("isClick") { setClick(it) }

        // maxHeight
        ui.gotHasString("maxHeight") { maxHeight = it.px }
        // scrollSize
        ui.gotHasString("scrollSize") { scrollBarSize = it.px }
        // fillViewport
        ui.gotHasBoolean("fillViewport") { isFillViewport = it }

        // isRefresh
        ui.gotHasBoolean("isRefresh")
        {
            isEnableRefresh = it
            if (!isEnableRefresh)
            {
                refreshLayout?.setOnClickListener(null)
                refreshLayout = null
            }
        }

        // refreshColor
        ui.gotHasString("refreshColor")
        {
            refreshColor = it.color
            refreshLayout?.setColorSchemeColors(refreshColor!!)
        }

        // onRefresh
        ui.gotHasFunction("onRefresh")
        { callback ->
            onRefresh = { callback.call(jsObject, null) }
        }

        // onScroll
        ui.gotHasFunction("onScroll")
        { callback ->
            onScroll = { callback.call(jsObject, JSArray(jsContext).push(it)) }
        }

        // 塞入子元素
        if (ui.contains("child"))
        {
            uiPage?.createUI(ui.gotObject("child"), this)
        } else if (ui.contains("childs"))
        {
            // 塞入lineLayout
            val lineUI = JSObject(jsContext)
                .set("type", "col")
                .set("width", "m")
                .set("height", "m")
                .set("childs", ui.gotArray("childs"))
            uiPage?.createUI(lineUI, this)
        }

        return if (ui.gotBoolean("isRefresh"))
        {
            // 塞入refresh
            refreshLayout = SwipeRefreshLayout(context)
            refreshLayout?.isEnabled = false
            val listParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            refreshLayout?.addView(this, listParams)
            refreshLayout!!
        } else
        {
            this
        }
    }

    /** 添加UI模板 */
    override fun addChild(ui: JSObject) { uiPage?.createUI(ui, getChildAt(0) as ViewGroup) }

    override var onSelect: (() -> Unit)? = null

    /********************************************** 点击 ****************************************/

    fun setClick(isClick: Boolean)
    {
        isAlpha = isClick
        isClickable = isClick
    }

    private var isAlpha = false

    override fun dispatchSetPressed(pressed: Boolean)
    {
        if (isAlpha) alpha = if (pressed || !isEnabled) 0.75f else 1f
        super.dispatchSetPressed(pressed)
    }

    override fun setEnabled(enabled: Boolean)
    {
        if (isAlpha) alpha = if (enabled) 1f else 0.75f
        super.setEnabled(enabled)
    }

    /*****************************************   滚动   ****************************************/

    var onScroll: ((t: Int)->Unit)? = null

    override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int)
    {
        super.onScrollChanged(l, t, oldl, oldt)
        onScroll?.invoke(t)
    }

    fun getScrollHeight(): Int
    {
        return try
        {
            getChildAt(0).height - height
        }catch (e: Exception)
        {
            0
        }
    }

    /***************************************** 加载刷新 ****************************************/

    private var refreshColor: Int? = null

    /** 刷新View */
    var refreshLayout: SwipeRefreshLayout? = null
        set(value)
        {
            if (isEnableRefresh)
            {
                field = value
                value?.isEnabled = true
                if (refreshColor != null)
                {
                    value?.setColorSchemeColors(refreshColor!!)
                } else
                {
                    value?.setColorSchemeColors(ContextCompat.getColor(context, R.color.colorPrimary))
                }
                // 刷新监听
                value?.setOnRefreshListener()
                {
                    postDelayed({ onRefresh?.invoke() }, 500)
                }
            } else
            {
                field = null
            }
        }

    fun refresh()
    {
        refreshLayout?.measure(0, 0)
        refreshLayout?.isRefreshing = true
    }

    fun setRefreshing(refreshing: Boolean)
    {
        refreshLayout?.isRefreshing = refreshing
    }

    private var isEnableRefresh = true

    /** 刷新监听 */
    var onRefresh: (() -> Unit)? = null

    private var maxHeight = 0

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
    {
        var heightSpec = heightMeasureSpec
        if (maxHeight > 0)
        {
            heightSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST)
        }
        super.onMeasure(widthMeasureSpec, heightSpec)
    }
}